package frame;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import frame.data.contrldata;

public class vwfileselect extends JDialog{
	public vwfileselect(master ma) {
		setTitle("VW-Edit 打开");
		setBounds(0, 0, 600, 400);
		setLocationRelativeTo(null);
		setIconImage(Toolkit.getDefaultToolkit().getImage("./material/logo/fvw.png"));
		setModal(true);
		setMinimumSize(new Dimension(600, 400));
		
		JFileChooser fc=new JFileChooser();
		fc.setDialogType(JFileChooser.OPEN_DIALOG);
		for(int i=0;i<filename.length;i++)fc.addChoosableFileFilter(getFileFilterOfNum(i));
		fc.setMinimumSize(new Dimension(600, 400));
		int a=fc.showOpenDialog(this);
		if(a==JFileChooser.APPROVE_OPTION) {
			try {
				String name=fc.getSelectedFile().getName();
				if(name.indexOf(".vw")==name.length()-3) {
					if(master.fl.openvwfile(fc.getSelectedFile().getPath().replaceAll("\\\\", "/"))) {
						master.t1l.addtexttoarea(ma);
						master.t2l.updatelinenum(ma);
						contrldata.filetab.updateUI();
					}
				}else new errortip("打开文件格式错误");
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	private String filename[][]= {{".vw","VW文件(*.vw)"}};
	private FileFilter getFileFilterOfNum(int num) {
		return new FileFilter() {
			@Override
			public String getDescription() {
				return filename[num][1];
			}
			@Override
			public boolean accept(File f) {
				if(f.getName().endsWith(filename[num][0])||f.isDirectory())return true;
				return false;
			}
		};
	}
}
